import { NestFactory } from '@nestjs/core';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
import { ValidationPipe } from '@nestjs/common';
import { join } from 'path';
import { AppModule } from './app.module';
import { AllResponseInterceptor } from './all-response.interceptor';
import { AllExceptionsFilter } from './all-exception.filter';
import { NestExpressApplication } from '@nestjs/platform-express';

async function bootstrap() {
  const app = await NestFactory.create<NestExpressApplication>(AppModule);

  app.useGlobalInterceptors(new AllResponseInterceptor()); // 设置一个全局的拦截器
  app.useGlobalPipes(
    new ValidationPipe({
      whitelist: true,
      forbidNonWhitelisted: true,
      transform: true,
      transformOptions: {
        enableImplicitConversion: true, // 如果设置为true，类转换器将尝试基于TS反射类型进行转换
      },
    }),
  ); // 设置字段验证插件
  app.useGlobalFilters(new AllExceptionsFilter()); // 对服务器端的异常做统一处理
  app.enableCors({ origin: true, credentials: true }); // 允许跨域，允许服务端向客户端写cookie
  // 设置静态目录
  app.useStaticAssets(join(__dirname, '..', 'public'), { prefix: '/static' });
  app.setGlobalPrefix('/api/v1');

  // https://docs.nestjs.com/openapi/introduction swagger文档
  const config = new DocumentBuilder()
    .setTitle('supply-os接口文档文档')
    .setDescription('一个swagger接口文档')
    .setVersion('1.0')
    .addBearerAuth()
    // .addTag('cats')
    .build();
  const document = SwaggerModule.createDocument(app, config);
  SwaggerModule.setup('docs', app, document); // 访问文档的时候访问/docs

  await app.listen(3010);
}
bootstrap();

// https://docs.nestjs.com/recipes/prisma parisma使用说明
